#include "mountVFS.h"
#include "dataStructuresHeader.h"
char* mountVFS(char *fileName)
{
	FILE *fp=fopen(fileName,"r+b");
	if(fp==NULL)
		return ERR_VFS_MOUNT_01;
	if(mh!=NULL)
		return ERR_VFS_MOUNT_03;
	int iterDescriptors=0;
	int check=-1;
	mh=(meta*)malloc(sizeof(meta));
	int i=0;
	fread(mh,sizeof(meta),1,fp);
	int currentFileDescriptors=mh->countOfFileDescriptors;
	int maxFileDescriptors=mh->maxDescriptors;
	int noOfBlocks=maxFileDescriptors/2;
	arr=(int*)malloc(sizeof(int)*noOfBlocks);
	h=(head*)malloc(sizeof(descriptors)*maxFileDescriptors+sizeof(char)*(noOfBlocks));
	fread(h,sizeof(descriptors)*maxFileDescriptors+sizeof(char)*(noOfBlocks),1,fp);
	check=0;
	h->desc=(descriptors*)malloc(sizeof(descriptors)*maxFileDescriptors);
	fseek(fp,sizeof(meta),SEEK_SET);
	check=-1;
	while(iterDescriptors<maxFileDescriptors)
	{
		fread(&(h->desc[iterDescriptors]),sizeof(descriptors),1,fp);
		iterDescriptors++;
	}
	fseek(fp,sizeof(meta)+sizeof(descriptors)*maxFileDescriptors,SEEK_SET);
	h->freeList=(char*)malloc(sizeof(char)*noOfBlocks);
	fread(h->freeList,sizeof(char)*noOfBlocks,1,fp);
	fseek(fp,sizeof(meta)+sizeof(descriptors)*maxFileDescriptors+sizeof(char)*noOfBlocks+maxBlockSize*(noOfBlocks-1),SEEK_SET);
	fread(arr,sizeof(arr),1,fp);
	check=0;
	int nAryRet=createnAryTree(currentFileDescriptors,h->desc); //create nAryTree
	if(nAryRet!=0)
		return "FAILURE <while creating nAryTree>";
	int hashTableRet=hashTableMethod(h->desc,iterDescriptors); //creates Hashtable
	if(hashTableRet!=0)
		return "FAILURE <while creating hashTable>";
	int bstRet=insertBSTWhileMount(maxFileDescriptors,h->desc);
	if(bstRet!=0)
		return "FAILURE <while creating BST>";
	//fclose(fp);
	if(check==0)
		return "SUCCESS";
	else
		return ERR_VFS_MOUNT_02;
}
